package gamer;


import java.util.LinkedList;

import tower.Tower;

import base.Base;
import dispatcher.InterfaceDispatcher;
/**
 * 
 * @author LocoRocoBattleStein
 *
 */
public class IA extends Gamer {
	private long lastAction;
	/**
	 * Constructor
	 * @param id
	 * @param pseudo
	 * @param dp
	 */
	public IA(int id, String pseudo, InterfaceDispatcher dp) {
		super(id, pseudo, dp);
		lastAction = System.currentTimeMillis();
	}
	
	public void play() {
		long currentTime = System.currentTimeMillis();
		if ((currentTime - lastAction) > 7000) {
			lastAction = currentTime;
			act();
		}
	}
	
	public void act() {
		
		int action = (int) (Math.random() * 3.);
		
		switch(action) {
		
		case 0 :
			System.out.println("IA UPGRADING TOWER");
			upgradeTower();
			break;
		case 1 :
			System.out.println("IA SETTING DEST BASE");
			setDestBase();
			break;
		case 2 :
			System.out.println("IA ADDING TOWER");
			addTower();
			break;
	
		}
		
	}
	
	public void upgradeTower() {
		LinkedList<Tower> listTower = bw.getTower_manager().getTowerListGamer(id);
		if(listTower.size() > 0) {
			int idTower = listTower.get((int) (Math.random()*(double)listTower.size())).getIdTower();
			int action = (int) (Math.random() * 4.);
			switch(action) {
			case 0 :
				if(money>80) {
					dp.upgradeTower(idTower, 0, 0, 0, -1);
				}
				break;
			case 1 :
				if(money>80) {
					dp.upgradeTower(idTower, 0, 0, -1, 0);
				}
				break;
			case 2 :
				if(money>100) {
					dp.upgradeTower(idTower, 0, 20, 0, 0);
				}
				break;
			case 3 :
				if(money>70) {
					dp.upgradeTower(idTower, -200, 0, 0, 0);
				}
				break;
			}
		}
	}
	
	public void setDestBase() {
		LinkedList<Base> IABase=bw.getBase_manager().getBaseListGamer(this.id);
		int numberSrc = (int) (Math.random()*(double)IABase.size());
	
		LinkedList<Base> neutralBase=bw.getBase_manager().getBaseListGamer(0);
		if(neutralBase.size()>0){
			int numberDst = (int) (Math.random()*(double)neutralBase.size());
			if(numberSrc != numberDst) dp.setDestBase(numberSrc, numberDst);
		}
	}
	
	public void addTower() {
		LinkedList<int[]> foothill=bw.getMap().getFoothillById(this.id, bw.getBase_manager());
		int num = (int) (Math.random()*(double)foothill.size());
		int idTower = ((int) (Math.random()*4.)) + 1;
		dp.addTower(idTower, this.id, foothill.get(num)[0], foothill.get(num)[1]);
	}
}

